package com.binvshe.binvshe.app.home;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.binvshe.binvshe.R;
import com.binvshe.binvshe.account.utils.FragmentUtils;
import com.binvshe.binvshe.activity.BannerEntity;
import com.binvshe.binvshe.activity.DetailActivity;
import com.binvshe.binvshe.activity.LocalImageHolderView;
import com.binvshe.binvshe.common.http.DataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.pachong.android.baseuicomponent.fragment.BaseFragment;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2017/2/9
 */

public class HotOpus1Fragment extends BaseFragment {
    @Bind(R.id.convenientBanner)
    ConvenientBanner mConvenientBanner;
    @Bind(R.id.frlytList)
    FrameLayout mFrlytList;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        FragmentUtils.replaceFragment(getChildFragmentManager(),R.id.frlytList,new HotOpusFragment());
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getBannerData("activity");

    }

    @Override
    public void onResume() {
        super.onResume();
        mConvenientBanner.startTurning(5000);
    }

    @Override
    public void onPause() {
        super.onPause();
        mConvenientBanner.stopTurning();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.frag_hot_new, container,false);
    }

    //设置banner数据
    private void setBannerData(final List<BannerEntity> bannerData) {
        if(mConvenientBanner!=null) {
            mConvenientBanner.setPages(
                    new CBViewHolderCreator<LocalImageHolderView>() {
                        @Override
                        public LocalImageHolderView createHolder() {
                            return new LocalImageHolderView();
                        }
                    }, bannerData).setPageIndicator(new int[]{R.mipmap.icon_nav_n, R.mipmap.icon_nav_p})
                    //设置指示器的方向
                    .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.ALIGN_PARENT_RIGHT);

            mConvenientBanner.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    String[]  strs=bannerData.get(position).getLink().split("id=");
                    int id = Integer.parseInt(strs[1]);
                    bannerData.get(position).getLink().startsWith("id=");
                    if(bannerData.get(position).getLink().startsWith("event://")){
                        DetailActivity.start(getContext(),id, bannerData.get(position).getImage(), bannerData.get(position).getName());
                    }else if(bannerData.get(position).getLink().startsWith("http://")){
                        WebView webView = new WebView(getActivity());
                        webView.getSettings().setDomStorageEnabled(true);
                        webView.getSettings().setDatabaseEnabled(true);
                        webView.getSettings().setJavaScriptEnabled(true);
                        webView.loadUrl(bannerData.get(position).getLink());
                    }else{

                    }
                }
            });
        }else {
            Log.d("HotOpus1Fragment", "1:" + 1);
        }

    }



    /**
     * 获取banner数据
     */
    private void getBannerData(String type) {
        MyHttpRequest request = new MyHttpRequest(getContext());
        String url = String.format(UrlCenter.BANNER_TYPE, type);

        request.get(url, null, new DataRequestListener<List<BannerEntity>>(BannerEntity.class) {
            @Override
            public void onStart(Context context, String url) {
                enableProgress(false);
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(List<BannerEntity> bannerEntity) {
                super.onSuccess(bannerEntity);
             //   Log.e("bannerEntity",bannerEntity.toString());
                setBannerData(bannerEntity);

            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }

}
